package com.tairan.chapter.flowable.service.ext;

import com.google.common.collect.Lists;
import com.tairan.chapter.flowable.service.ext.impl.FlowGroupQueryImpl;
import com.tairan.chapter.flowable.service.ext.impl.FlowUserQueryImpl;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.idm.api.*;
import org.flowable.idm.engine.impl.IdmIdentityServiceImpl;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import java.util.List;

/**
 * 扩展用户权限
 */
public class FlowIdentityServiceImpl extends IdmIdentityServiceImpl {

    @Override
    public UserQuery createUserQuery() {
        return new FlowUserQueryImpl();
    }

    @Override
    public GroupQuery createGroupQuery() {
        return new FlowGroupQueryImpl();
    }

    @Override
    public boolean checkPassword(String userId, String password) {
        return executeCheckPassword(userId, password);
    }

    @Override
    public List<Group> getGroupsWithPrivilege(String name) {
        System.out.println("getGroupsWithPrivilege:" + name);
        List<Group> groups = Lists.newArrayList();
        List<PrivilegeMapping> privilegeMappings = getPrivilegeMappingsByPrivilegeId(name);
        privilegeMappings.forEach(item -> {
            if (item.getGroupId() != null) {
                Group group = new GroupEntityImpl();
                group.setId(item.getGroupId());
                group.setName(item.getGroupId());
                groups.add(group);
            }
        });
        return groups;
    }

    @Override
    public List<User> getUsersWithPrivilege(String name) {
        List<User> users = Lists.newArrayList();
        List<PrivilegeMapping> privilegeMappings = getPrivilegeMappingsByPrivilegeId(name);
        privilegeMappings.forEach(item -> {
            if (item.getUserId() != null) {
                User user = new UserEntityImpl();
                user.setId(item.getUserId());
                user.setLastName(item.getUserId());
                users.add(user);
            }
        });
        return users;
    }

    @Override
    public User newUser(String userId) {
        throw new FlowableException("LDAP identity service doesn't support creating a new user");
    }

    @Override
    public void saveUser(User user) {
        throw new FlowableException("LDAP identity service doesn't support saving an user");
    }

    @Override
    public NativeUserQuery createNativeUserQuery() {
        throw new FlowableException("LDAP identity service doesn't support native querying");
    }

    @Override
    public void deleteUser(String userId) {
        throw new FlowableException("LDAP identity service doesn't support deleting an user");
    }

    @Override
    public Group newGroup(String groupId) {
        throw new FlowableException("LDAP identity service doesn't support creating a new group");
    }

    @Override
    public NativeGroupQuery createNativeGroupQuery() {
        throw new FlowableException("LDAP identity service doesn't support native querying");
    }

    @Override
    public void saveGroup(Group group) {
        throw new FlowableException("LDAP identity service doesn't support saving a group");
    }

    @Override
    public void deleteGroup(String groupId) {
        throw new FlowableException("LDAP identity service doesn't support deleting a group");
    }

    protected boolean executeCheckPassword(final String userId, final String password) {

        return true;
    }
}
